/**
 * @file drv_pwm.c
 * @brief HAL CUBEMX DRIVER
 * 
 * @author dalin (dalin@open-robot.com)
 * @version 1.0
 * @date 2023-05-10
 * 
 * @copyright Copyright (c) 2023  Open Robot Tech.co, Ltd
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-05-10 <td>1.0     <td>dalin     <td>初始化
 * </table>
 */

#include "drv_tim.h"

#ifdef USE_TIM_PWM_DRIVER || USE_TIM_ENCODER_DRIVER
#define USE_TIM_DRIVER
#endif

#ifdef USE_TIM_DRIVER

#include "mlog.h"

__weak TIM_HandleTypeDef htim1;
__weak TIM_HandleTypeDef htim2;
__weak TIM_HandleTypeDef htim3;
__weak TIM_HandleTypeDef htim4;
__weak TIM_HandleTypeDef htim5;
__weak TIM_HandleTypeDef htim6;
__weak TIM_HandleTypeDef htim7;
__weak TIM_HandleTypeDef htim8;


/**
 * @brief Get the tim handle object
 * 
 * @param  tim              : tim number
 * 
 * @return TIM_HandleTypeDef* 
 */
TIM_HandleTypeDef *  get_tim_handle(timNumber_e tim)
{
    TIM_HandleTypeDef *handle = NULL;
    
    /* 映射TIM */
    switch (tim)
    {
    case DRV_TIM1:
        handle = &htim1;
        break;
    case DRV_TIM2:
        handle = &htim2;
        break;
    case DRV_TIM3:
        handle = &htim3;
        break;
    case DRV_TIM4:
        handle = &htim4;
        break;
    case DRV_TIM5:
        handle = &htim5;
        break;
    case DRV_TIM6:
        handle = &htim6;
        break;
    case DRV_TIM7:
        handle = &htim7;
        break;
    case DRV_TIM8:
        handle = &htim8;
        break;
    
    default:
        log_error("tim %d error", tim);

        break;
    }

    return handle;
}

/**
 * @brief Get the tim channel object
 * 
 * @param  channel          : channel number
 * 
 * @return uint32_t 
 */
uint32_t  get_tim_channel(timChannel_e channel)
{
    uint32_t ch = 0xFFFFFFFF;

    /* 映射TIM通道 */
    switch (channel)
    {
    case TIM_CH1:
        ch = TIM_CHANNEL_1;
        break;

    case TIM_CH2:
        ch = TIM_CHANNEL_2;
        break;

    case TIM_CH3:
        ch = TIM_CHANNEL_3;
        break;

    case TIM_CH4:
        ch = TIM_CHANNEL_4;
        break;

    case TIM_CH_ALL:
        ch = TIM_CHANNEL_ALL;
        break;

    default:
        log_error("channel:%d error", channel);

        break;
    }

    return ch;
}


#endif /* USE_TIM_DRIVER */
